<?php 
include(APP_HOME."/MPDF56/mpdf.php");
include(APP_HOME."/modules/systemapi/controllers/Barcode.php");
if(!class_exists('MAIN_Controller_Action')) Zend_Loader :: loadClass('MAIN_Controller_Action');
class Front_PrintPaymentController extends MAIN_Controller_Action {
	
	public function printpaymentAction() {
		if(!class_exists('Zend_Auth'))Zend_loader :: loadClass('Zend_Auth');
		$params   = $this->getParams();
		$generic = $this->getGeneric();
		
		$paymentId = $generic->getPaymentIDbyPreautID($params['preauth_id']);
		$payment_generic = System_Controller::getGeneric("payment","front");
		$printpayment_generic = System_Controller::getGeneric("printpayment","front");
		$view = $this->_getView();
		$today = getToday();
		$payment_datetime	=	date('Y-m-d H:i:s');
		$auth = Zend_Auth :: getInstance();
    	$identity = $auth->getIdentity();
    	
    	//$payment_detail = $generic->getPaymentDetail($params['payment_id']);
    	$payment_detail = $generic->getPaymentDetail($paymentId);
    	
    	$preauthData = $payment_generic->getPreAuthById($payment_detail['preauth_id'],$identity->userid);
    	$policyData = $payment_generic->getPolicyById($preauthData['0']['policyid']);
    	//$barcodeData = $printpayment_generic->Barcode39('PM0000000424', 500, 80, 100, 'JPEG', '');
    	//$barcodeData = ''; 
    	//ImageJPEG ($barcodeData, "", 100);
    	$PlanData = $payment_generic->getPlan2catByPlanId($policyData['plan_id'],$preauthData['0']['policyid']); 
    	$PlanData['opd'] = number_format($PlanData['opd'],2,'.',',');
    	$PlanData['rb'] = number_format($PlanData['rb'],2,'.',',');
    	$PlanData['er'] = number_format($PlanData['er'],2,'.',',');
    	$claimGetCover	=	$payment_generic->getClaimCover();
		
		$img_path	=	APP_HOME.'/modules/systemapi/templates/default/images';
		$barcode_path = APP_HOME.'/modules/systemapi/templates/default/images/barcode';
		$barcode_url = APP_URL.'/modules/systemapi/templates/default/images/barcode';
		
		//_print($PlanData);exit();
		//$view->assign("paymentId",$params['payment_id']);
		$view->assign("preauthData",$preauthData['0']);
		$view->assign("today",$today);
    	$view->assign("payment_datetime",$payment_datetime);
    	$view->assign("policyData",$policyData);
    	$view->assign("claimGetCover",$claimGetCover);
    	$view->assign("PlanData",$PlanData);
    	$view->assign("payment_detail",$payment_detail);
		//$view->output('pdf/printpayment.tpl');

    	//$paymentId	=	$params['payment_id'];
    	$preauth_id	=	$preauthData[0]['preauth_id'];
    	$preauth_datetime	=	$preauthData[0]['preauth_datetime'];
    	//_print($preauthData[0]['preauth_id']);exit();
		 if($policyData['customer_type_desc']	==	"PA"){
			$img = 'title_pa.png';
		} else if($policyData['customer_type_desc']	==	"Health"){
			$img = 'title_health.png';
		} 
		
		// set Barcode39 object
		$bc = new Barcode39($paymentId);		
		$bc->draw($barcode_path.'/'.$paymentId.'.gif');
		
		$html = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=TIS-620" />
</head>
<body style="background-color:#ffffff;" >
	<div id="print_payment_head" style="border-style:solid; border-width:0px; width:700px; border-color: Red; ">
		<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px;" >			
			<tr> 
				<td width="95" style="text-align:right; vertical-align:bottom;"> ระบบ :</td>
				<td width="160" style="text-align:left; vertical-align:bottom;"> Payment</td>				
				<td width="240" rowspan="2" valign="middle" align="center" style="font-size:18px;"> Health Bebefit Consultants </td>
				<td width="100" rowspan="4" valign="middle" align="center"style="font-size:18px;"><img src="'.APP_URL.'/modules/systemapi/templates/default/images/logo.png" </td>
			</tr>
			<tr>
				<td width="95" style="text-align:right; vertical-align:bottom;"> วันทำรายการ :</td>
				<td width="160" style="text-align:left;">'.$today.'</td>
				
				
			</tr>
			<tr> 
				<td width="95" style="text-align:right; vertical-align:middle;"> หน่วยงาน :</td>
				<td width="150" style="text-align:left;">'.$identity->group_name.'</td>
				<td width="240" rowspan="2" valign="middle" align="center" style="font-size:14px;"><B>Payment ID</B> <br><img src="'.$barcode_url.'/'.$paymentId.'.gif" /> </td>												  
			</tr>
			<tr> 
				<td width="95" style="text-align:right; vertical-align:middle;"> ประเภทกรมธรรม์ :</td>
				<td width="150" style="text-align:left; font-size:22px;">'.$policyData['customer_type_desc'].'</td>
			</tr>			
		</table>
	</div>
	
	<div id="print_payment_PolicyINFO" style="border-style:solid; border-width:0px; width:700px; border-color: blue; ">
		<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >
			<tr> 
				<td align="center" colspan="4" style="font-size:16px;" ><U><B> ข้อมูลกรมธรรม์ </B></U></td>			
			</tr>
			<tr> 
				<td width="150" style="text-align:right;">ชื่อผู้เอาประกัน :</td>
				<td width="200" style="text-align:left;">'.$policyData['name'].' '.$policyData['surname'].' </td>
				<td width="150" style="text-align:right;">บัตรประชาชน :</td>
				<td width="200" style="text-align:left;">'.$policyData['natid'].' </td>			
			</tr>
			<tr> 
				<td style="text-align:right;">เลขที่กรมธรรม์ :</td>
				<td style="text-align:left;"> '.$policyData['policy_no'].'</td>
				<td style="text-align:right;">เลขที่บัตรประกัน :</td>
				<td style="text-align:left;"> '.$policyData['cardno'].'</td>			
			</tr>
			<tr> 
				<td style="text-align:right;">วันเริ่มคุ้มครอง :</td>
				<td style="text-align:left;">'.$policyData['effective'].' </td>
				<td style="text-align:right;">วันสิ้นสุดความคุ้มครอง :</td>
				<td style="text-align:left;">'.$policyData['expiry'].' </td>			
			</tr>
			<tr> 
				<td style="text-align:right;">สถานะกรมธรรม์ :</td>
				<td style="text-align:left;">'.$policyData['polstatus_desc'].' </td>
				<td style="text-align:right;">แผนความคุ้มครอง :</td>
				<td style="text-align:left;"> '.$policyData['plan'].'</td>			
			</tr>
			<tr>
				<td align="right">Deductible :</td>
				<td>'.number_format ( $policyData['deductible'], 2, ".", "," ).'</td>
				<td align="right">Co-Payment :</td>
				<td>'.number_format ( $policyData['copayment'], 2, ".", "," ).'</td>
			</tr>
		</table>
	</div>
	
	<div style="width:700px; float:left; border-style: dashed; border-width:0px; margin-top:15px;">
		<div id="print_payment_preauthINFO" style="border-style:solid; border-width:0px; width:350px; border-color: blue; float:left;">
			<table width="350" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;" >
				<tr> 
					<td align="center" colspan="2" style="font-size:16px;" ><U><B> ข้อมูล Pre-auth</B></U></td>			
				</tr>
				<tr> 
					<td width="150" style="text-align:right;">pre-auth ID:</td>
					<td width="200" style="text-align:left;">'.$preauth_id.' </td>
				</tr>
				<tr>	
					<td width="150" style="text-align:right;">วันที่ทำ Pre-auth :</td>
					<td width="200" style="text-align:left;">'.$preauth_datetime.' </td>
				</tr>
				<tr> 
					<td style="text-align:right;">รหัสผู้ใช้ :</td>
					<td style="text-align:left;"> '.$identity->userid.'</td>
				</tr>
				<tr>
					<td style="text-align:right;">หน่วยงาน :</td>
					<td style="text-align:left;"> '.$identity->group_name.'</td>			
				</tr>
				<tr> 
					<td style="text-align:right;">ตำแหน่ง :</td>
					<td style="text-align:left;">'.$identity->usertype_description.' </td>
				</tr>
			</table>
		</div>
		<div id="print_payment_admisINFO" style="border-style:solid; border-width:0px; width:350; border-color: Red; float:right">
			<table width="350" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;" >
				<tr>
					<td colspan="2" align="center" style="font-size:16px;"><U><B>ข้อมูลการรักษา</B></U></td>
				</tr>
				<tr>
					<td width="140" height="32" align="right" valign="middle">วันเกิดอุบัติเหตุ :</td>
					<td align="left" valign="middle">'.getDateFormat2($payment_detail['acc_date']).'</td>
				</tr>
				<tr>
					<td width="140" height="32" align="right" valign="middle">วันเข้ารักษา :</td>
					<td align="left" valign="middle">'.getDateFormat2($payment_detail['admis_date']).'</td>
				</tr>
				<tr>
					<td width="140" height="32" align="right" valign="middle">ประเภทอุบัติเหตุ :</td>
					<td align="left" valign="middle">'.$payment_detail['payment_type_desc'].'</td>
				</tr>
			</table>
		</div>
	</div>
							
	<div id="print_payment_indicationAdmis" style="border-style:solid; border-width:0px; width:700; border-color: Red; float:none;">
		<table width="700" align="left" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >
			<tr>
				<td align="center" style="font-size:16px;"><U><B>สาเหตุการรักษา</B></U></td>
			</tr>
			<tr>
				<td height="100" align="left" valign="top">'.$payment_detail['indication_admit'].'</td>
			</tr>
		</table>
	</div>				
						
	<div id="print_payment_Chargedetail" style="border-style:solid; border-width:0px; width:700px; border-color: green; float:left; ">
		<table width="250" align="center" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px; ">
			<tr>			
				<td colspan="2" align="center" style="font-size:16px;" ><U><B>รายละเอียดค่าใช้จ่าย</B></U></td>
			</tr>
				<td width="50%"align="right" valign="top">Charge :</td>
				<td align="right" valign="top">'.number_format ($payment_detail['charge'], 2, ".", "," ).'  บาท</td>	
			<tr>
				<td align="right" valign="top">Discount :</td>
				<td align="right" valign="top">'.number_format ($payment_detail['discount'], 2, ".", "," ).'  บาท</td>
			</tr>
			<tr>
				<td align="right" valign="top">Deductible :</td>
				<td align="right" valign="top">'.number_format ($payment_detail['deductible'], 2, ".", "," ).'  บาท</td>
			</tr>
			<tr>
				<td align="right" valign="top">Co-Payment :</td>
				<td align="right" valign="top">'.number_format ($payment_detail['copayment'], 2, ".", "," ).'  บาท</td>
			</tr>
			<tr>
				<td style="font-size:18px;" align="right" valign="top"><B>Pre approved :</B></td>
				<td style="font-size:18px;" align="right" valign="top"><B>'.number_format ($payment_detail['pre_approved'], 2, ".", "," ).'  บาท</B></td>
			</tr>
			<tr>
				<td style="font-size:18px;" align="right" valign="top"><B>Non Covered :</B></td>
				<td style="font-size:18px;" align="right" valign="top"><B>'.number_format ($payment_detail['non_covered'], 2, ".", "," ).'  บาท</B></td>
			</tr>
		</table>
	</div>

		
	

</body>
</html>
';
		
	/*	//$this->render('../../../../MPDF56/mpdf.php');
		$mpdf=new mPDF('utf-8','A4','14','sarabun',5,5,5,5,10,10);
		
		$mpdf->SetDisplayMode('fullpage');
		// LOAD a stylesheet
		$stylesheet = file_get_contents(APP_HOME."/modules/systemapi/templates/default/css/print.css");
		$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text
		
		 if($policyData['customer_type_desc']	==	"PA"){
			$mpdf->WriteHTML($html);
		} else if($policyData['customer_type_desc']	==	"Health"){
			$mpdf->WriteHTML($html2);
		} 
		$mpdf->WriteHTML($html);
	
		
		$SmPDF = $mpdf->Output('', 'S');
	*/
		$view->assign("html",$html);
		$SmPDF = $html;
		$SmPDF = base64_encode($SmPDF);
		$DataRet = $generic->updatePaymentImage($paymentId,$SmPDF);	
				
		$view->output('pdf/printpayment.tpl');
		//$mpdf->Output(); 
		//exit();
		
}

public function printpaymenttransAction() {
	$params   = $this->getParams();
	$generic = $this->getGeneric();
	$DataRet = $generic->getPaymentImage($params['payment_id']);
	$DataRet = base64_decode($DataRet[payment_img]);
	_print($DataRet); exit();

}
public function paymentcancelAction() {
	$params   = $this->getParams();
	$generic = $this->getGeneric();
	$payment_generic = System_Controller::getGeneric("payment","front");
	$printpayment_generic = System_Controller::getGeneric("printpayment","front");
	
	$auth = Zend_Auth :: getInstance();
	$identity = $auth->getIdentity();
	$today = getToday();

	$cancelId = $printpayment_generic->getCancelId();
	$printpayment_generic->insertCancel($cancelId,$params['payment_id'],$identity->userid);
	$payment_detail = $generic->getPaymentDetail($params['payment_id']);	 
	$preauth_detail = $payment_generic->getPreAuthById($payment_detail['preauth_id'],$identity->userid);
	//_print($payment_detail);exit();
	$policyData = $payment_generic->getPolicyById($preauth_detail['0']['policyid']);
	
	$html_cancel = '
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta http-equiv="Content-Type" content="text/html; charset=TIS-620" />
			</head>
			<body style="background-color:#ffffff;" >
				<div id="print_payment_head" style="border-style:solid; border-width:0px; width:700px; border-color: Red; ">
					<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px;" >			
						<tr> 
							<td width="95" style="text-align:right; vertical-align:bottom;"> ระบบ :</td>
							<td width="160" style="text-align:left; vertical-align:bottom;"> Cancel</td>				
							<td width="240" rowspan="2" valign="middle" align="center" style="font-size:18px;"> Health Bebefit Consultants </td>
							<td width="100" rowspan="4" valign="middle" align="center"style="font-size:18px;"><img src="'.APP_URL.'/modules/systemapi/templates/default/images/logo.png" </td>
						</tr>
						<tr>
							<td width="95" style="text-align:right; vertical-align:bottom;"> วันทำรายการ :</td>
							<td width="160" style="text-align:left;">'.$today.'</td>
							
							
						</tr>
						<tr> 
							<td width="95" style="text-align:right; vertical-align:middle;"> หน่วยงาน :</td>
							<td width="150" style="text-align:left;">'.$identity->group_name.'</td>
							<td width="240" rowspan="2" valign="middle" align="center" style="font-size:18px;"><B>Cancel ID</B> <br>'.$cancelId. '</td>												  
						</tr>
						<tr> 
							<td width="95" style="text-align:right; vertical-align:middle;"> ประเภทกรมธรรม์ :</td>
							<td width="150" style="text-align:left; font-size:22px;">'.$policyData['customer_type_desc'].'</td>
						</tr>			
					</table>
				</div>
				
				
				
				<div style="width:700px; float:left; border-style: dashed; border-width:0px; margin-top:15px;">
					<div id="print_payment_preauthINFO" style="border-style:solid; border-width:0px; width:350px; border-color: blue; float:left;">
						<table width="350" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;" >
							<tr> 
								<td align="center" colspan="2" style="font-size:16px;" ><U><B> ข้อมูล Pre-auth</B></U></td>			
							</tr>
							<tr> 
								<td width="150" style="text-align:right;">pre-auth ID:</td>
								<td width="200" style="text-align:left;">'.$preauth_detail['0']['preauth_id'].' </td>
							</tr>
							<tr>	
								<td width="150" style="text-align:right;">วันที่ทำ Pre-auth :</td>
								<td width="200" style="text-align:left;">'.$preauth_detail['0']['preauth_datetime'].' </td>
							</tr>
							<tr> 
								<td style="text-align:right;">รหัสผู้ใช้ :</td>
								<td style="text-align:left;"> '.$preauth_detail['0']['userid'].'</td>
							</tr>
							<tr>
								<td style="text-align:right;">หน่วยงาน :</td>
								<td style="text-align:left;"> '.$preauth_detail['0']['group_name'].'</td>			
							</tr>
							<tr> 
								<td style="text-align:right;">ตำแหน่ง :</td>
								<td style="text-align:left;">'.$preauth_detail['0']['username'].' </td>
							</tr>
						</table>
					</div>
										
					<div id="print_payment_preauthINFO" style="border-style:solid; border-width:0px; width:350px; border-color: blue; float:left;">
						<table width="350" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:0px;" >
							<tr> 
								<td align="center" colspan="2" style="font-size:16px;" ><U><B> ข้อมูล Payment</B></U></td>			
							</tr>
							<tr> 
								<td width="150" style="text-align:right;">payment ID:</td>
								<td width="200" style="text-align:left;">'.$payment_detail['payment_id'].' </td>
							</tr>
							<tr>	
								<td width="150" style="text-align:right;">วันที่ทำ Pre-auth :</td>
								<td width="200" style="text-align:left;">'.$payment_detail['payment_datetime'].' </td>
							</tr>
							<tr> 
								<td style="text-align:right;">รหัสผู้ใช้ :</td>
								<td style="text-align:left;"> '.$payment_detail['userid'].'</td>
							</tr>
							<tr>
								<td style="text-align:right;">หน่วยงาน :</td>
								<td style="text-align:left;"> '.$payment_detail['group_name'].'</td>			
							</tr>
							<tr> 
								<td style="text-align:right;">ตำแหน่ง :</td>
								<td style="text-align:left;">'.$payment_detail['user_name'].' </td>
							</tr>
						</table>
					</div>
				</div>
										
				<div id="print_payment_PolicyINFO" style="border-style:solid; border-width:0px; width:700px; border-color: blue; ">
					<table width="700" valign="top" border="1" cellpadding="3" cellspacing="0" style="font-size:14px; margin-top:15px;" >
						<tr> 
							<td align="center" colspan="4" style="font-size:16px;" ><U><B> ข้อมูลกรมธรรม์ </B></U></td>			
						</tr>
						<tr> 
							<td width="150" style="text-align:right;">ชื่อผู้เอาประกัน :</td>
							<td width="200" style="text-align:left;">'.$policyData['name'].' '.$policyData['surname'].' </td>
							<td width="150" style="text-align:right;">บัตรประชาชน :</td>
							<td width="200" style="text-align:left;">'.$policyData['natid'].' </td>			
						</tr>
						<tr> 
							<td style="text-align:right;">เลขที่กรมธรรม์ :</td>
							<td style="text-align:left;"> '.$policyData['policy_no'].'</td>
							<td style="text-align:right;">เลขที่บัตรประกัน :</td>
							<td style="text-align:left;"> '.$policyData['cardno'].'</td>			
						</tr>
						<tr> 
							<td style="text-align:right;">วันเริ่มคุ้มครอง :</td>
							<td style="text-align:left;">'.$policyData['effective'].' </td>
							<td style="text-align:right;">วันสิ้นสุดความคุ้มครอง :</td>
							<td style="text-align:left;">'.$policyData['expiry'].' </td>			
						</tr>
						<tr> 
							<td style="text-align:right;">สถานะกรมธรรม์ :</td>
							<td style="text-align:left;">'.$policyData['polstatus_desc'].' </td>
							<td style="text-align:right;">แผนความคุ้มครอง :</td>
							<td style="text-align:left;"> '.$policyData['plan'].'</td>			
						</tr>
						<tr>
							<td align="right">Deductible :</td>
							<td>'.number_format ( $policyData['deductible'], 2, ".", "," ).'</td>
							<td align="right">Co-Payment :</td>
							<td>'.number_format ( $policyData['copayment'], 2, ".", "," ).'</td>
						</tr>
					</table>
				</div>
				
			
			</body>
			</html>
	';

	$SmPDF = $html_cancel;
	$SmPDF = base64_encode($SmPDF);
	$DataRet = $generic->updateCancelImage($cancelId,$SmPDF);
	_print($html_cancel);
	exit();
}

public function printcanceltransAction() {
	$params   = $this->getParams();
	$generic = $this->getGeneric();
	$DataRet = $generic->getCancelImage($params['payment_id']);
	$DataRet = base64_decode($DataRet[cancel_img]);
	_print($DataRet); exit();

}
		
}
?>